package com.inspector.app.ui.flight.maplayer;


import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.inspector.app.R;
import com.mapsforge.map.EfbMap;

import java.util.ArrayList;


/**
 * [地图选择]
 */
public class SwitchMapLayerAdapter extends RecyclerView.Adapter<SwitchMapLayerAdapter.SwitchVH> {

    private final ArrayList<SwitchItem> mList;
    private final OnItemClickListener onItemClickListener;

    public SwitchMapLayerAdapter(ArrayList<SwitchItem> mList, OnItemClickListener onItemClickListener) {
        this.mList = mList;
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public SwitchVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return SwitchVH.createAirportVH(viewGroup);
    }

    @Override
    public void onBindViewHolder(SwitchVH viewHolder, int position) {
        viewHolder.bindItem(mList.get(position), position, onItemClickListener);
    }

    @Override
    public int getItemCount() {
        return (mList != null && mList.size() > 0) ? mList.size() : 0;
    }

    public static class SwitchVH extends RecyclerView.ViewHolder {


        private final ImageView itemIV;
        private final TextView itemTV;
        private SwitchItem switchItem;
        private OnItemClickListener itemClickListener;

        public SwitchVH(final View itemView) {
            super(itemView);
            View itemRootView = itemView.findViewById(R.id.itemRootView);
            itemIV = itemView.findViewById(R.id.itemIV);
            itemTV = itemView.findViewById(R.id.itemTV);

            itemRootView.setOnClickListener(v -> {
                if (itemClickListener != null) {
                    itemClickListener.onItemClick(itemView, switchItem);
                }
            });
        }

        public void bindItem(SwitchItem item, int position, OnItemClickListener onItemClickListener) {
            if (item == null) return;
            itemIV.setImageResource(item.imgSrc);
            itemTV.setText(item.text);
            if (item.clickType == item.mapType) {
                itemTV.setBackgroundColor(Color.parseColor("#0779cf"));
            } else {
                itemTV.setBackgroundColor(Color.parseColor("#4d4d4d"));
            }
            switchItem = item;
            itemClickListener = onItemClickListener;
        }

        public static SwitchVH createAirportVH(ViewGroup viewGroup) {
            View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.maplayer_switch_item, null);
            return new SwitchVH(inflate);
        }

    }

    public static class SwitchItem {
        public String text;
        public Integer imgSrc;
        public EfbMap.MapType mapType;
        public EfbMap.MapType clickType;

        public SwitchItem(String text, Integer imgSrc, EfbMap.MapType mapType, EfbMap.MapType clickType) {
            this.text = text;
            this.imgSrc = imgSrc;
            this.mapType = mapType;
            this.clickType = clickType;
        }
    }

    public interface OnItemClickListener {
        void onItemClick(@Nullable View view, @Nullable SwitchItem item);
    }
}
